<?php

namespace App\Sys\Models;

use App\Models\ApiSoftDeletes;
use App\Models\BaseModel;
use Illuminate\Support\Facades\DB;

class SysI18nResourceModel extends BaseModel
{
    use ApiSoftDeletes;
    protected $table = 'sys_i18n_resource';

    const LANG_LIST = ['zh_tw', 'en_us'];

    /**
     * 确认机器翻译结果
     * @param array $resourceIds
     */
    public function confirmMachineTrans(array $resourceIds)
    {
        $resourceIds = implode(',', $resourceIds);
        foreach (self::LANG_LIST as $lang) {
            $sql = "update sys_i18n_resource r left join sys_i18n_machine_trans t on t.resource_id = r.id
                set r.$lang = t.$lang  where r.id in ($resourceIds) and r.$lang = '' and t.$lang != ''";
            DB::update($sql);
        }
    }

    /**
     * 分页查询国际化资源
     * @param $appId string 应用ID
     * @param $module string 模块ID
     * @param $key string 键
     * @param $content string 中文内容
     * @param $auditLang string 审核语言
     * @param $pageSize 页码
     * @return array
     */
    public function listResource($appId, $module, $key, $content, $auditLang, $pageSize)
    {
        $where = [
            'r.status'=>0
        ];
        if ($appId) {
            $where['r.app_id'] = $appId;
        }
        if ($module) {
            $where['r.module'] = $module;
        }
        if ($key) {
            $where['r.key'] = $key;
        }
        if ($content) {
            $where[0] = ['r.zh_cn', 'like', '%'.$content.'%'];
        }
        if ($auditLang) {
            $where['r.'.$auditLang] = '';
        }
        return $this->db('r')
            ->leftJoin('sys_i18n_machine_trans as t', 'r.id', '=', 't.resource_id')
            ->where($where)
            ->orderByRaw('r.id desc')
            ->selectRaw("r.id,r.app_id,r.module,r.`key`,r.code,
                r.zh_cn,r.zh_tw,r.en_us,r.ru_ru,r.ar_ar,r.de_de,r.ja_jp,r.ko_kr,r.ms_my,r.es_es,
                r.id_id,r.th_th,r.fr_fr,r.pt_pt,
                t.zh_tw as t_zh_tw,t.en_us as t_en_us,
                t.ru_ru as t_ru_ru,t.ar_ar as t_ar_ar,t.de_de as t_de_de,t.ja_jp as t_ja_jp,
                t.ko_kr as t_ko_kr,t.ms_my as t_ms_my,t.es_es as t_es_es,
                t.id_id as t_id_id,t.th_th as t_th_th,t.fr_fr as t_fr_fr,t.pt_pt as t_pt_pt")
            ->paginate($pageSize)
            ->toArray();
    }

    /**
     * 获取语言国际化数据
     * @param $appId 应用ID
     * @param $locale 本地化语言
     * @return array
     */
    public function getAppLocale($appId, $locale)
    {
        $where = [
            'r.app_id'=>$appId,
            'r.status'=>0
        ];
        return $this->db('r')
            ->leftJoin('sys_i18n_machine_trans as t', 'r.id', '=', 't.resource_id')
            ->where($where)
            ->orderByRaw('r.code,r.id')
            ->selectRaw("r.module,r.`key`,r.code,
                case when r.$locale != '' then r.$locale else ifnull(t.$locale, r.$locale) end as $locale")
            ->get()
            ->toArray();
    }

    /**
     * 获取系统所有的资源文件
     * @param $appId string 应用ID
     * @param $module string 模块
     * @return \Illuminate\Support\Collection
     */
    public function getAppResource($appId, $module)
    {
        $where = [
            'r.app_id'=>$appId,
            'r.status'=>0
        ];
        if ($module) {
            $where['r.module'] = $module;
        }
        return $this->db('r')
            ->leftJoin('sys_i18n_machine_trans as t', 'r.id', '=', 't.resource_id')
            ->where($where)
            ->orderByRaw('r.code,r.id')
            ->selectRaw("r.module,r.`key`,r.code,r.zh_cn,
                case when r.zh_tw != '' then r.zh_tw else ifnull(t.zh_tw, r.zh_tw) end as zh_tw,
                case when r.en_us != '' then r.en_us else ifnull(t.en_us, r.en_us) end as en_us,
                case when r.ru_ru != '' then r.ru_ru else ifnull(t.ru_ru, r.ru_ru) end as ru_ru,
                case when r.ar_ar != '' then r.ar_ar else ifnull(t.ar_ar, r.ar_ar) end as ar_ar,
                case when r.de_de != '' then r.de_de else ifnull(t.de_de, r.de_de) end as de_de,
                case when r.ja_jp != '' then r.ja_jp else ifnull(t.ja_jp, r.ja_jp) end as ja_jp,
                case when r.ko_kr != '' then r.ko_kr else ifnull(t.ko_kr, r.ko_kr) end as ko_kr,
                case when r.ms_my != '' then r.ms_my else ifnull(t.ms_my, r.ms_my) end as ms_my,
                case when r.es_es != '' then r.es_es else ifnull(t.es_es, r.es_es) end as es_es,
                case when r.id_id != '' then r.id_id else ifnull(t.id_id, r.id_id) end as id_id,
                case when r.th_th != '' then r.th_th else ifnull(t.th_th, r.th_th) end as th_th,
                case when r.fr_fr != '' then r.fr_fr else ifnull(t.fr_fr, r.fr_fr) end as fr_fr,
                case when r.pt_pt != '' then r.pt_pt else ifnull(t.pt_pt, r.pt_pt) end as pt_pt")
            ->get()
            ->toArray();
    }

    /**
     * 获取需要翻译的列表
     * @param $lang
     * @param $limit
     * @return \Illuminate\Support\Collection
     */
    public function getTransList($lang, $limit)
    {
        return $this->db('r')
            ->leftJoin('sys_i18n_machine_trans as t', 't.resource_id', '=', 'r.id')
            ->where([
                'r.status'=>0,
                '0'=>['r.zh_cn', '<>', '']
            ])
            ->whereRaw("(t.`$lang` = '' or t.`$lang` is null)")
            ->selectRaw('r.id,t.resource_id as trans_id,r.zh_cn')
            ->limit($limit)
            ->get();
    }

}
